﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;
using LR4.Models;

namespace LR4.DAO
{
    public class MainModelDAO : DAO
    {
        public List<MainModel> GetAllRecords()  // Выборка записей из ContactInfo, Property, Type - основной GET-метод
        {
            Connect();
            List<MainModel> MainModelList = new List<MainModel>();

            try
            {
                SqlCommand command = new SqlCommand("SELECT ContactInfo.ID, ContactInfo.Name, " +
                "ContactInfo.Phone, Property.Square, Property.Address, Property.Details, " +
                "Type.Property_type FROM Type INNER JOIN Property ON Type.ID_t = Property.Type_id " +
                "INNER JOIN ContactInfo ON Property.Id_f = ContactInfo.ID", Connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    MainModel mainModel = new MainModel();
                    mainModel.ID = Convert.ToInt32(reader["ID"]);
                    mainModel.Name = Convert.ToString(reader["Name"]);
                    mainModel.Phone = Convert.ToString(reader["Phone"]);
                    mainModel.Square = Convert.ToString(reader["Square"]);
                    mainModel.Address = Convert.ToString(reader["Address"]);
                    mainModel.Details = Convert.ToString(reader["Details"]);
                    mainModel.Property_type = Convert.ToString(reader["Property_type"]);
                    MainModelList.Add(mainModel);
                }
                reader.Close();
            }

            catch (Exception)
            {
            }

            finally
            {
                Disconnect();
            }
            return MainModelList;
        }
    }
}
